<?php

namespace zhtfrontend\modules\parentsgrowth\controllers;

use yii\db\Query;
use zhtfrontend\modules\parentsgrowth\components\rest\ActiveController;
use common\components\helpers\Fun;
use common\models\datas\Student;
use doumiaoduo\aliyun\OssuploaderHelper;
use yii\console\Exception;
use yii\data\ActiveDataProvider;
use yii\web\UploadedFile;
use zhtfrontend\models\datas\ClassLesson;
use zhtfrontend\models\datas\ClassManage;
use zhtfrontend\models\datas\ClassManageAttribute;
use zhtfrontend\models\datas\ClassManageTeacher;
use zhtfrontend\models\datas\ClassManageTime;
use zhtfrontend\models\datas\ClassStudent;
use zhtfrontend\models\datas\LessonMedia;
use zhtfrontend\models\datas\LessonStudents;
use zhtfrontend\models\datas\Medias;
use zhtfrontend\models\datas\StudentCheck;
use zhtfrontend\models\datas\Teacher;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class GrowthworksController extends ActiveController

{
    private $_config;
    public $modelClass = 'zhtfrontend\models\datas\ClassManage';

    public $serializer = [
        'class' => 'zhtfrontend\modules\parentsgrowth\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }

    public function actionWorks(){
        $data = \Yii::$app->request->post();
        $student_id=ArrayHelper::keyExists('student_id', $data) ? $data['student_id'] : '';
        $type=ArrayHelper::keyExists('types', $data) ? $data['types'] : '';//all-时间轴；time-时间；category-分类
        $time=ArrayHelper::keyExists('time', $data) ? $data['time'] : '';//all-时间轴；time-时间；category-分类
        $cate_id=ArrayHelper::keyExists('cate_id', $data) ? $data['cate_id'] : '';
        $cate_name=ArrayHelper::keyExists('course_name', $data) ? $data['course_name'] : '';
        if (empty($student_id)){
            return ['statusCode' => 0, 'message' => 'error', 'result' => '学生信息不存在'];
        }else{
            //$lesson_students_query=LessonStudents::find()->where(['student_id'=>$student_id]);
            $lesson_students_query=(new Query())->select('*')->from('{{%lesson_students}}')->where(['student_id'=>$student_id]);
            if(isset($type)){
                switch ($type){
                    case 'all':
                        $lesson_students=$lesson_students_query->all(Fun::getSiteDb());
                        break;
                    case 'time':
                        $time_array=json_decode($time, true);//出来的是数组的形式，第一个是年，第二个是月，第三个是日
                        $time_query=(new Query())->select('cl.lesson_id as lesson_id,cl.class_date as class_date')
                            ->from('{{%class_lesson}} cl');
                        $lesson_students_query=$lesson_students_query->leftJoin(['time_query'=>$time_query],'{{%lesson_students}}.lesson_id=time_query.lesson_id');
                        if (!empty($time_array[0])&& !empty($time_array[1]) && empty($time_array[2])){ //只有年月
                            $time_start=strtotime($time_array[0]."-".$time_array[1]."-".'01' ."00:00:00");
                            $time_end=strtotime($time_array[0]."-".$time_array[1]."-".'31' ."23:59:59");
                        }elseif (!empty($time_array[0])&& empty($time_array[1]) && empty($time_array[2])){//只有年
                            $time_start=strtotime($time_array[0]."-".'01'."-".'01' ."00:00:00");
                            $time_end=strtotime($time_array[0]."-".'12'."-".'31' ."23:59:59");
                        }else{//年月日都有
                            $time_start=strtotime($time_array[0]."-".$time_array[1]."-".$time_array[2]."00:00:00");
                            $time_end=strtotime($time_array[0]."-".$time_array[1]."-".$time_array[2]."23:59:59");
                        }
                        if (isset($time_array) && !empty($time_array)){
                            $lesson_students_query=$lesson_students_query->andWhere(['between','time_query.class_date',$time_start,$time_end]);
                        }
                        $lesson_students=$lesson_students_query->all(Fun::getSiteDb());
                        break;
                    case 'category':
                        $cate_query=(new Query())->select('g.cat_id as cate_id,cl.lesson_id as lesson_id')
                            ->from('{{%class_lesson}} cl')
                            ->leftJoin('{{%class_manage_goods}} cmg','cl.class_id=cmg.class_id')
                            ->leftJoin('{{%goods}} g','cmg.goods_id=g.id');
                        $lesson_students_query=$lesson_students_query->leftJoin(['cate_query'=>$cate_query],'{{%lesson_students}}.lesson_id=cate_query.lesson_id');
                        if(isset($cate_id)&&!empty($cate_id)){
                            $lesson_students_query=$lesson_students_query->andWhere(['cate_query.cate_id'=>$cate_id]);
                        }
                        if(isset($cate_name)&&!empty($cate_name)){
                            $category=(new Query())->select('*')->from('{{%cate}}')->where(['name'=>$cate_name])->one();
                            $lesson_students_query=$lesson_students_query->andWhere(['cate_query.cate_id'=>$category['id']]);
                        }
                        $lesson_students=$lesson_students_query->all(Fun::getSiteDb());
                        break;
                    default:
                        $lesson_students=$lesson_students_query->all(Fun::getSiteDb());
                }
            }else{
                $lesson_students=$lesson_students_query->all(Fun::getSiteDb());
            }
            $growth_works_array=[];
            foreach ($lesson_students as $key=>$val){
                $class_info=(new Query())->select('*')->from('{{%class_lesson}}')->where(['lesson_id'=>$val['lesson_id']])->one(Fun::getSiteDb());
                $lesson_media_teacher=(new Query())->select('*')->from('{{%lesson_media}}')->where(['lesson_id'=>$val['lesson_id'],'student_id'=>$student_id,'is_refouce'=>0])->one(Fun::getSiteDb());
                //课堂花絮
                if (isset($lesson_media_teacher) && !empty($lesson_media_teacher)){
                    $media_catkin_array=[
                        'images'=>(new Query())->select('*')->from('{{%medias}}')->where(['object_id'=>$lesson_media_teacher['id'],'type'=>1])->orderBy('id DESC')->limit(4)->all(Fun::getSiteDb()),
                        'video'=>(new Query())->select('*')->from('{{%medias}}')->where(['object_id'=>$lesson_media_teacher['id'],'type'=>2])->orderBy('id DESC')->one(Fun::getSiteDb()),
                    ];
                    $catkin=[
                        'title'=>'课堂花絮',
                        'array'=>$media_catkin_array,
                    ];
                }else{
                    $media_catkin_array=[];
                    $catkin=[
                        'title'=>'课堂花絮',
                        'array'=>$media_catkin_array,
                    ];
                }
                //课下练习反馈
                $lesson_media_student=(new Query())->select('*')->from('{{%lesson_media}}')->where(['lesson_id'=>$val['lesson_id'],'student_id'=>$student_id,'is_refouce'=>1])->one(Fun::getSiteDb());
                if (isset($lesson_media_student) && !empty($lesson_media_student)){
                    $media_practice_array=[
                        'images'=>(new Query())->select('*')->from('{{%medias}}')->where(['object_id'=>$lesson_media_student['id'],'type'=>1])->orderBy('id DESC')->limit(4)->all(Fun::getSiteDb()),
                        'video'=>(new Query())->select('*')->from('{{%medias}}')->where(['object_id'=>$lesson_media_student['id'],'type'=>2])->orderBy('id DESC')->one(Fun::getSiteDb()),
                    ];
                    $practice=[
                        'title'=>'课下反馈',
                        'array'=>$media_practice_array,
                    ];
                }else{
                    $media_practice_array=[];
                    $practice=[
                        'title'=>'课下反馈',
                        'array'=>$media_practice_array,
                    ];
                }
                //查看当前图片和视频的数量
                $all_count=count($catkin['array'])+count($practice['array']);
                if ($all_count>3){
                    $moreWorksShow=false;
                    $iconShow=true;
                    $buttonText='展开更多';
                }elseif ($all_count==0){
                    $moreWorksShow=true;
                    $iconShow=false;
                    $buttonText='暂时没有信息';
                }else{
                    $moreWorksShow=true;
                    $iconShow=false;
                    $buttonText='收起';
                }
                $growth_works_array[$key]=[
                    'id'=>$key,
                    'active_bar'=>'',
                    'today'=>'',
                    'day'=>date("d",$class_info['class_date']),
                    'month'=>date("m",$class_info['class_date']),
                    'worksList'=>[
                        'catkin'=>$catkin,//课堂花絮
                        'practice'=>$practice,//课下练习反馈
                    ],
                    'moreWorksShow'=>$moreWorksShow,
                    'iconShow'=> $iconShow,
                    'buttonText'=>$buttonText,
                    'count'=>$all_count,
                ];
            }
            $cate=(new Query())->select('id,name')->from('{{%cate}}')->where(['parent_id'=>0])->all();
            $items=[
                'cate'=>$cate,
                'growth_works_array'=>$growth_works_array,
            ];
            return ['statusCode' => 200, 'message' => 'error', 'items' => $items];
        }
    }


}